package net.alche.al.util.far;

import java.io.File;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import net.alche.al.util.Far;
import net.alche.al.util.M;
import net.alche.al.util.far.pojo.ResultPack;
import net.alche.al.util.far.pojo.VueFunction;

// FaBaseHttp
@SuppressWarnings("all")
public class FaBaseHttp {
  private ExecutorService exeService;

  public FaBaseHttp() {

  }

  // 线程池
  public ExecutorService getExeSer() {
    if (this.exeService == null) {
      this.exeService = Executors.newFixedThreadPool(50);
    }
    return this.exeService;
  }

  // 1.timeout
  public VueFunction timeOut(int millieSec) {
    VueFunction fun = new VueFunction();
    getExeSer().submit(new Runnable() {
      @Override
      public void run() {
        if (millieSec > 0) {
          try {
            Thread.sleep(millieSec);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
        fun.call();
      }
    });
    return fun;
  }

  public String getSync(String url, Map<String, Object> params) {
    String res = null;
    try {
      res = HttpUtil.get(url + M.toUrlParam(params));
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return res;
  }

  public String httpPost(String url, Object params) {
    String res = null;
    try {
      if (params instanceof Map mpp) {
        if (mpp.get("__heads") != null) {
          Map headMap = (Map) mpp.get("__heads");
          mpp.remove("__heads");
          return postSyncHeader(url, params, headMap);
        }
      }
      res = HttpUtil.post(url, Far.tl.toJsonTxt(params));
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return res;
  }

  public String postSync(String url, Object params) {
    return httpPost(url, params);
  }

  public String postSyncHeader(String url, Object params, Map<String, String> header) {
    String pack = null;
    try {
      HttpRequest request = HttpUtil.createPost(url);
      request.addHeaders(header);
      request.body(Far.tl.toJsonTxt(params));
      request.timeout(1000 * 15);
      pack = request.execute().body();
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return pack;
  }

  public String delSync(String url) {
    String res = null;
    try {
      res = HttpRequest.delete(url).timeout(15000).execute().body();
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return res;
  }

  public static String postUploadFileSync(String url, String filePath, Map<String, Object> mp) {
    HttpResponse res = null;
    try {
      HttpRequest rrq = HttpRequest.post(url).form("file", new File(filePath));
      if (mp != null) {
        for (String key : mp.keySet()) {
          rrq = rrq.form(key, mp.get(key));
        }
      }
      res = rrq.execute();
    } catch (Exception ex) {
      ex.printStackTrace();
      return null;
    }
    return res.body();
  }
  public static String postUploadFileSyncByByts(String url, String fileName,byte[] byts, Map<String, Object> mp) {
    HttpResponse res = null;
    try {
      HttpRequest rrq = HttpRequest.post(url).form("file", byts, fileName); //.form("file", new File(filePath));
      if (mp != null) {
        for (String key : mp.keySet()) {
          rrq = rrq.form(key, mp.get(key));
        }
      }
      res = rrq.execute();
    } catch (Exception ex) {
      ex.printStackTrace();
      return null;
    }
    return res.body();
  }
}